package com.se2.antiqueexhibition.Controller;


import com.se2.antiqueexhibition.Mapper.AdminActivityMapper;
import com.se2.antiqueexhibition.Service.AdminActivityService;
import com.se2.antiqueexhibition.pojo.AdminActivity;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import java.util.List;


@Controller
@RequestMapping("/adminActivity")
public class AdminActivityController {

    @Resource
    private AdminActivityService adminActivityService;

    @GetMapping("/list")
    public String listAdminActivity(Model model){
        List<AdminActivity> adminActivity = adminActivityService.getActivity();
        model.addAttribute("adminActivity", adminActivity);
        return "adminActivity-management";

    }



    @PostMapping("/insert")
    public String insert(@ModelAttribute AdminActivity adminActivity, Model model) {
        try{
            adminActivityService.insert(adminActivity);
            model.addAttribute("message","活动添加成功");
            model.addAttribute("success", true);
            return "redirect:list";
        }catch(Exception e){
            model.addAttribute("message","活动添加失败");
            model.addAttribute("success", false);
            model.addAttribute("adminActivity", adminActivity);
            return "adminActivity-from";
        }
    }

    @PostMapping("/update")
    public String update(@ModelAttribute AdminActivity adminActivity, Model model) {
        try{
            adminActivityService.update(adminActivity);
            model.addAttribute("message","活动更新成功");
            model.addAttribute("success", true);
            return "redirect:list";
        }catch(Exception e){
            model.addAttribute("message","活动更新失败");
            model.addAttribute("success", false);
            model.addAttribute("adminActivity", adminActivity);
            return "adminActivity-from";
        }
    }

    @GetMapping("/delete")
    public String delete(@RequestParam int id, Model model) {
        try{
            adminActivityService.delete(id);
            model.addAttribute("message","活动删除成功");
            model.addAttribute("success", true);
        } catch (Exception e) {
            model.addAttribute("message","活动删除失败");
            model.addAttribute("success", false);

        }
        return "adminActivity:list";

    }

}
